package views.games;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

import core.constants.Constants;
import views.util.GameColors;

public class MemoriaOrdinataView extends GameView implements ActionListener {

	private JPanel main;
	private GridBagConstraints gbc = new GridBagConstraints();
	private Timer timer = new Timer(1000, this);
	private JButton[] shuffledButton;
	private int[] shuffledNumbers;
	private int difficulty;
	private int showTime = 3;
	private int maxRound = Constants.getRounds();
	private KeyListener kl; // serve per rimuovere il keyListener una volta
							// premuto invio per avviare il gioco
	private ActionListener currentActionListener;

	public MemoriaOrdinataView(int[] shuffledNumbers) {
		super(GameColors.VERDE, GameColors.VERDE_CHIARO, "INVIO PER INIZIARE",
				"PUNTEGGIO: 0", "NUMERO CASELLE: 4");
		difficulty = shuffledNumbers.length;
		this.shuffledNumbers = shuffledNumbers;
		shuffledButton = new JButton[difficulty];
		for (int i = 0; i < difficulty; i++) {
			shuffledButton[i] = new JButton("" + shuffledNumbers[i]);
			shuffledButton[i].setPreferredSize(new Dimension(60, 60));
			shuffledButton[i].setBackground(GameColors.VERDE_CHIARO);
			shuffledButton[i].setForeground(GameColors.VERDE_CHIARO); // per
																		// rendere
																		// i
																		// numeri
																		// invisibili
			shuffledButton[i].setBorderPainted(false);
			shuffledButton[i].setFont(new Font("Segoe UI", Font.BOLD, 20));
		}
		main = new JPanel(new GridBagLayout());
		main.setBackground(GameColors.VERDE);
		add(main, BorderLayout.CENTER);
		gbc.insets = new Insets(5, 5, 5, 5);
		switch (difficulty) {
		case 4:
			buildFourSchema();
			break;
		case 5:
			buildFiveSchema();
			break;
		case 6:
			buildSixSchema();
			break;
		case 7:
			buildSevenSchema();
			break;
		case 8:
			buildEightSchema();
			break;
		default:
			buildNineSchema();
		}

	}

	public void checkFocusMain() {
		main.requestFocus();
	}

	public void refresh(int[] shuffledNumbers) {
		maxRound--;
		if (maxRound != 0) {
			remove(main);
			main = new JPanel(new GridBagLayout());
			main.setBackground(GameColors.VERDE);
			add(main, BorderLayout.CENTER);
			this.difficulty = shuffledNumbers.length;
			this.shuffledNumbers = shuffledNumbers;
			this.shuffledButton = new JButton[difficulty];
			for (int i = 0; i < difficulty; i++) {
				shuffledButton[i] = new JButton("" + shuffledNumbers[i]);
				shuffledButton[i].setPreferredSize(new Dimension(60, 60));
				shuffledButton[i].setBackground(GameColors.VERDE_CHIARO);
				shuffledButton[i].setBorderPainted(false);
				shuffledButton[i].setFont(new Font("Segoe UI", Font.BOLD, 20));
			}
			switch (difficulty) {
			case 4:
				buildFourSchema();
				break;
			case 5:
				buildFiveSchema();
				break;
			case 6:
				buildSixSchema();
				break;
			case 7:
				buildSevenSchema();
				break;
			case 8:
				buildEightSchema();
				break;
			default:
				buildNineSchema();

			}
			other.setText("NUMERO CASELLE: " + difficulty);
		} else
			gameEnded();
	}

	public void buildFourSchema() {
		int index = 0;

		// RIEMPI PRIMA RIGA
		gbc.gridy = 0;
		for (int i = 0; i < 2; i++) {
			gbc.gridx = i;
			main.add(shuffledButton[index], gbc);
			index++;
		}
		// RIEMPI SECONDA RIGA
		gbc.gridy = 1;
		for (int i = 0; i < 2; i++) {
			gbc.gridx = i;
			main.add(shuffledButton[index], gbc);
			index++;
		}
	}

	public void buildFiveSchema() {
		int index = 0;

		// RIEMPI PRIMA RIGA
		gbc.gridy = 0;
		for (int i = 0; i < 3; i += 2) {
			gbc.gridx = i;
			main.add(shuffledButton[index], gbc);
			index++;
		}
		// RIEMPI SECONDA RIGA
		gbc.gridx = 1;
		gbc.gridy = 1;
		main.add(shuffledButton[index], gbc);
		index++;
		// RIEMPI TERZA RIGA
		gbc.gridy = 2;
		for (int i = 0; i < 3; i += 2) {
			gbc.gridx = i;
			main.add(shuffledButton[index], gbc);
			index++;
		}
	}

	public void buildSixSchema() {
		int index = 0;

		// RIEMPI PRIMA RIGA
		gbc.gridx = 2;
		gbc.gridy = 0;
		main.add(shuffledButton[index], gbc);
		index++;
		// RIEMPI SECONDA RIGA
		gbc.gridy = 1;
		for (int i = 1; i < 3; i++) {
			gbc.gridx = i;

			main.add(shuffledButton[index], gbc);
			index++;
		}
		// RIEMPI TERZA RIGA
		gbc.gridy = 2;
		for (int i = 0; i < 3; i++) {
			gbc.gridx = i;
			main.add(shuffledButton[index], gbc);
			index++;
		}

	}

	public void buildSevenSchema() {
		int index = 0;

		// RIEMPI PRIMA RIGA
		gbc.gridy = 0;
		for (int i = 1; i < 3; i++) {
			gbc.gridx = i;
			main.add(shuffledButton[index], gbc);
			index++;
		}
		// RIEMPI SECONDA RIGA
		gbc.gridy = 1;
		for (int i = 0; i < 3; i++) {
			gbc.gridx = i;
			main.add(shuffledButton[index], gbc);
			index++;
		}
		// RIEMPI TERZA RIGA
		gbc.gridy = 2;
		for (int i = 0; i < 2; i++) {
			gbc.gridx = i;
			main.add(shuffledButton[index], gbc);
			index++;
		}
	}

	public void buildEightSchema() {
		int index = 0;

		// RIEMPI TUTTE LE RIGHE
		for (int i = 0; i < 4; i++) {
			gbc.gridy = 0;
			gbc.gridx = i;
			main.add(shuffledButton[index], gbc);
			index++;
			gbc.gridy = 1;
			main.add(shuffledButton[index], gbc);
			index++;
		}
	}

	public void buildNineSchema() {
		int index = 0;

		// RIEMPI TUTTE LE RIGHE
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				gbc.gridx = j;
				gbc.gridy = i;
				main.add(shuffledButton[index], gbc);
				index++;
			}
		}
	}


	public void addActionListenerToGame(ActionListener al) {
		if (al != null)
			currentActionListener = al;
		for (int i = 0; i < difficulty; i++) {
			shuffledButton[i].addActionListener(this.currentActionListener);

		}
	}

	public void addKeyLiestenerToGame(KeyListener kl) {
		main.addKeyListener(kl);
	}

	@Override
	public void gameEnded() {
		System.out.println("Memoria ordinata end");
		other.setVisible(false);
		gameClock.setVisible(false);
		for (int i = 0; i < difficulty; i++) {
			main.remove(shuffledButton[i]);
		}
		JLabel end = newLabel("Partita Conclusa!", GameColors.VERDE_CHIARO);
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.gridwidth = 4;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(0, 0, 0, 0);
		main.add(end, gbc);
		gbc.gridy = 1;
		main.add(score, gbc);
		JButton restart = newButton("Ricomincia");
		JButton menu = newButton("Menu Giochi");
		gbc.gridy = 2;
		gbc.gridwidth = 2;
		gbc.gridx = 0;
		main.add(restart, gbc);
		gbc.gridx = 2;
		main.add(menu, gbc);
		restart.addActionListener(currentActionListener);
		menu.addActionListener(currentActionListener);
	}

	public void hideNumbers() {
		for (int i = 0; i < difficulty; i++) {
			shuffledButton[i].setForeground(GameColors.VERDE_CHIARO);
		}
	}

	public void showNumbers() {
		for (int i = 0; i < difficulty; i++) {
			shuffledButton[i].setForeground(Color.BLACK);
		}
	}

	public void removeKeyListenerFromGame() {
		for (int i = 0; i < difficulty; i++) {
			shuffledButton[i].removeKeyListener(kl);
		}

	}

	public void startTimer() {
		showTime = 3;
		gameClock.setText("" + showTime);
		timer.start();
	}

	public void stopTimer() {
		timer.stop();
		gameClock.setText("DARE RISPOSTA");
	}

	public int getDifficulty() {
		return difficulty;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {

		showTime--;
		gameClock.setText("" + showTime);
	}

	public void colorCellsWin() {
		System.out.println("CALL colorCellsWin");
		for (int i = 0; i < difficulty; i++) {
			shuffledButton[i].setBackground(Color.YELLOW);
		}

	}

	public void disableAll() {
		for (int i = 0; i < difficulty; i++) {
			shuffledButton[i].setEnabled(false);
		}
	}

	public void enableAll() {
		for (int i = 0; i < difficulty; i++) {
			shuffledButton[i].setEnabled(true);
		}
	}

	public JButton[] getShufflelButtons() {
		return shuffledButton;
	}

}
